﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Utilities
{
    /// <summary>
    /// 验证字符串是否合法
    /// </summary>
    public class ValidateUtil
    {
        #region 构造函数
        /// <summary>构造函数</summary>
        public ValidateUtil()
        {

        }
        #endregion

        private static Regex RegNumber = new Regex("^[0-9]+$");
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$ 
        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

        //验证Email地址 
        public static bool IsValidEmail(string strIn)
        {
            // Return true if strIn is in valid e-mail format. 
            return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
        //dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。 
        public static string MDYToDMY(String input)
        {
            return Regex.Replace(input, "\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b", "${day}-${month}-${year}");
        }
        //验证是否为小数 
        public static bool IsValidDecimal(string strIn)
        {
            return Regex.IsMatch(strIn, @"[0].\d{1,2}|[1]");
        }
        //验证是否为电话号码 
        public static bool IsValidTel(string strIn)
        {
            return Regex.IsMatch(strIn, @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
        }
        //验证年月日 
        //		public bool IsValidDate1(string strIn) 
        //		{ 
        //			return Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$"); 
        //		} 
        //验证后缀名 
        public bool IsValidPostfix(string strIn)
        {
            return Regex.IsMatch(strIn, @"\.(?i:gif|jpg)$");
        }
        //验证字符是否再4至12之间 
        public static bool IsValidByte(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[a-z]{4,12}$");
        }
        //验证IP 
        public static bool IsValidIp(string strIn)
        {
            return Regex.IsMatch(strIn, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
        }
        //验证日期
        public static bool IsValidDate(string Date)
        {
            bool bValid = Regex.IsMatch(Date, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$");
            return (bValid && Date.CompareTo("1753-01-01") >= 0);

        }
        /// <summary> 
        /// 有效URL检验 
        /// </summary> 
        public static bool IsValidUrl(string strIn)
        {
            return Regex.IsMatch(strIn, @"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
        }
        /// <summary> 
        /// 有效邮政编码检验 
        /// </summary> 
        public static bool IsValidPcode(string strIn)
        {
            return Regex.IsMatch(strIn, @"^(\d{6})+$");
        }
        /// <summary> 
        /// 有效整数检验 
        /// </summary> 
        public static bool IsValidInt(string strIn)
        {
            //[0-9]也可 
            return Regex.IsMatch(strIn, "^(\\d)+$");
        }


        /// <summary>
        /// 获取字符串或汉字的长度
        /// </summary>
        /// <param name="str">字符串或汉字</param>
        /// <returns>长度</returns>
        public static int GetStrLength(string str)
        {
            if (str == null || str == string.Empty)
            {
                return 0;
            }
            else
            {
                return System.Text.Encoding.Default.GetByteCount(str);
            }
        }

        /// <summary>
        /// 获取随即数
        /// </summary>
        /// <returns></returns>
        public static string GetCode()
        {
            string checkCode = string.Empty;
            Random ro = new Random(System.Guid.NewGuid().GetHashCode());
            checkCode = DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next(99999);

            return checkCode;
        }

        /// <summary>
        /// 判断时候是数字组合
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static bool IsNumber(string strDate)
        {
            if (strDate == null)
            {
                return false;
            }
            if (strDate.Equals(string.Empty))
            {
                return false;
            }

            Regex numRegex = new Regex(@"0*[0-9][0-9]*$");
            return numRegex.IsMatch(strDate);
        }

        /// <summary>
        /// 判断是不是合法的email
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static bool IsEmail(string strDate)
        {
            Regex mailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            return mailRegex.IsMatch(strDate);
        }

        /// <summary>
        /// 判断是不是合法的日期
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static bool IsDate(string strDate)
        {
            if (strDate == null)
            {
                return false;
            }
            Regex dateRegex = new Regex(@"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$");
            return dateRegex.IsMatch(strDate);
        }

        /// <summary>
        /// 昵称规则
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNickName(string str)
        {
            Regex oReg = new Regex(@"^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$");
            if (oReg.IsMatch(str))
            {
                return true;
            }
            return false;
        }


        /// <summary> 
        /// 有效密码检验 
        /// </summary> 
        public static bool IsValidPassword(string strIn)
        {
            if (strIn.Length < 6 || strIn.Length > 20)
            {
                return false;
            }
            else
            {
                return Regex.IsMatch(strIn, "^[A-Za-z0-9]+$");
            }
        }


        /// <summary> 
        /// 有效URL检验 
        /// </summary> 
        public static bool IsValidUrl_2(string strIn)
        {
            return Regex.IsMatch(strIn, @"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
        }
        /// <summary> 
        /// 有效日期检验　如2005-9-2 
        /// 基本上把闰年和2月等的情况都考虑进去了 
        /// </summary> 
        public static bool IsValidDate_2(string strIn)
        {
            return Regex.IsMatch(strIn, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }
        /// <summary> 
        /// 有效长日期检验　如2005-9-2 13:20:37 
        /// 基本上把闰年和2月等的情况都考虑进去了 
        /// </summary> 
        public static bool IsValidDateLong(string strIn)
        {
            return Regex.IsMatch(strIn, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
        }
        /// <summary> 
        /// 有效身份证检验 
        /// </summary> 
        public static bool IsValidIDCard(string strIn)
        {
            return Regex.IsMatch(strIn, @"^(\d{18}|\d{15})+$");
        }

        /// <summary> 
        /// 有效英文检验 
        /// </summary> 
        public static bool IsValidEnglish(string strIn)
        {
            return Regex.IsMatch(strIn, "^([A-Za-z])+$");
        }
        /// <summary> 
        /// 有效中文检验 
        /// </summary> 
        public static bool IsValidChinese(string strIn)
        {
            return Regex.IsMatch(strIn, "^([\u4e00-\u9fa5])+$");
        }



        /// <summary> 
        /// 有效英文-数字检验 
        /// </summary> 
        public static bool IsValidEnglishInt(string strIn)
        {
            //[A-Za-z|0-9]也可以 
            return Regex.IsMatch(strIn, "^[A-Za-z0-9]+$");
        }
        /// <summary> 
        /// 有效中文-数字检验 
        /// </summary> 
        public static bool IsValidChineseInt(string strIn)
        {
            return Regex.IsMatch(strIn, "^[0-9\u4e00-\u9fa5]+$");
        }
        /// <summary> 
        /// 有效中文-英文-数字检验 
        /// </summary> 
        public static bool IsValidChineseEnglishInt(string strIn)
        {
            return Regex.IsMatch(strIn, "^[A-Za-z0-9\u4e00-\u9fa5]+$");
        }


        public static bool CompareByteArray(byte[] obj1, byte[] obj2)
        {
            if (obj1.Length != obj1.Length)
                return false;

            for (int i = 0; i < obj1.Length; i++)
            {
                if (obj1[i] != obj2[i])
                    return false;
            }
            return true;
        }
    }
}
